<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>问道工具 - 瓢把子统计</title>
    <style>
        /* 基础样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
            position: relative;
        }

        /* 移除全局页面水印，改为区域水印 */

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        /* 选项卡样式 */
        .tab-container {
            background: #f8f9fa;
            border-bottom: 1px solid #e9ecef;
            position: relative;
        }

        /* 选项卡导航区域水印 - 将由JavaScript动态创建 */

        .tab-nav {
            display: flex;
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .tab-nav li {
            flex: 1;
        }

        .tab-nav button {
            width: 100%;
            padding: 20px;
            border: none;
            background: transparent;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            color: #6c757d;
            transition: all 0.3s ease;
            border-bottom: 3px solid transparent;
        }

        .tab-nav button:hover {
            background: rgba(102, 126, 234, 0.1);
            color: #667eea;
        }

        .tab-nav button.active {
            background: white;
            color: #667eea;
            border-bottom-color: #667eea;
        }

        /* 选项卡内容 */
        .tab-content {
            padding: 30px;
            min-height: 400px;
            position: relative;
        }

        /* 选项卡内容区域水印 - 将由JavaScript动态创建 */

        /* 确保激活的选项卡面板内容在水印之上 */
        .tab-panel.active {
            position: relative;
            z-index: 2;
        }

        .tab-panel {
            display: none;
        }

        .tab-panel.active {
            display: block;
            animation: fadeIn 0.3s ease-in-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* 瓢把子统计样式 */
        .statistics-title {
            font-size: 24px;
            font-weight: 600;
            color: #333;
            margin-bottom: 20px;
            text-align: center;
        }

        /* 作者署名样式 */
        .author-info {
            font-size: 14px;
            color: #6c757d;
            text-align: center;
            margin-bottom: 20px;
            font-style: italic;
            opacity: 0.8;
        }

        /* 队伍管理区域 */
        .team-management {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 12px;
        }

        .team-controls {
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .add-team-btn {
            padding: 10px 20px;
            background: #28a745;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .add-team-btn:hover {
            background: #218838;
            transform: translateY(-1px);
        }

        .current-team-info {
            font-size: 16px;
            font-weight: 500;
            color: #333;
        }

        /* 队伍列表容器 */
        .teams-container {
            margin-bottom: 30px;
        }

        /* 队伍卡片 */
        .team-card {
            background: white;
            border: 2px solid #e9ecef;
            border-radius: 12px;
            padding: 25px;
            margin-bottom: 20px;
            transition: all 0.3s ease;
            position: relative;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        }

        .team-card:hover {
            border-color: #667eea;
            box-shadow: 0 6px 20px rgba(102, 126, 234, 0.15);
            transform: translateY(-2px);
        }

        /* 非激活状态的队伍卡片轻微背景色 */
        .team-card:not(.active) {
            background: rgba(248, 249, 250, 0.8);
        }

        .team-card.active {
            border-color: #667eea;
            background: rgba(102, 126, 234, 0.05);
            box-shadow: 0 6px 20px rgba(102, 126, 234, 0.2);
        }

        /* 队伍头部 */
        .team-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }

        .team-name {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            cursor: pointer;
            padding: 5px 10px;
            border-radius: 6px;
            transition: all 0.3s ease;
        }

        .team-name:hover {
            background: rgba(102, 126, 234, 0.1);
        }

        .team-name.editing {
            background: white;
            border: 2px solid #667eea;
            outline: none;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
            animation: pulse 1.5s infinite;
        }

        @keyframes pulse {
            0% { box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2); }
            50% { box-shadow: 0 0 0 6px rgba(102, 126, 234, 0.1); }
            100% { box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2); }
        }

        .team-actions {
            display: flex;
            gap: 8px;
        }

        .team-action-btn {
            padding: 8px 16px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 13px;
            font-weight: 600;
            transition: all 0.3s ease;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            min-width: 60px;
        }

        .team-action-btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
        }

        .reset-btn {
            background: #6c757d;
            color: white;
        }

        .reset-btn:hover {
            background: #5a6268;
        }

        .rename-btn {
            background: #17a2b8;
            color: white;
        }

        .rename-btn:hover {
            background: #138496;
        }

        .delete-btn {
            background: #dc3545;
            color: white;
        }

        .delete-btn:hover {
            background: #c82333;
        }

        /* 回合水平布局 */
        .rounds-row {
            display: flex;
            gap: 12px;
            align-items: center;
            flex-wrap: wrap;
            justify-content: flex-start;
            margin-bottom: 20px;
        }

        .round-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            min-width: 70px;
            flex: 0 0 auto;
        }

        .round-label {
            font-size: 12px;
            font-weight: 500;
            color: #666;
            margin-bottom: 8px;
            text-align: center;
        }

        .round-options {
            display: flex;
            gap: 4px;
        }

        .option-button {
            padding: 8px 12px;
            border: 2px solid #e9ecef;
            border-radius: 6px;
            background: white;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s ease;
            min-width: 40px;
        }

        .option-button:hover {
            border-color: #667eea;
            color: #667eea;
        }

        .option-button.selected {
            background: #667eea;
            color: white;
            border-color: #667eea;
        }

        .option-button.selected.come {
            background: #28a745;
            border-color: #28a745;
        }

        .option-button.selected.not-come {
            background: #dc3545;
            border-color: #dc3545;
        }

        /* 非激活状态下选中按钮的增强样式 */
        .team-card:not(.active) .option-button.selected.come {
            background: #28a745;
            border-color: #28a745;
            box-shadow: 0 2px 8px rgba(40, 167, 69, 0.4);
            transform: scale(1.05);
        }

        .team-card:not(.active) .option-button.selected.not-come {
            background: #dc3545;
            border-color: #dc3545;
            box-shadow: 0 2px 8px rgba(220, 53, 69, 0.4);
            transform: scale(1.05);
        }

        /* 队伍结果显示 */
        .team-result {
            margin-top: 20px;
            padding: 20px;
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-radius: 10px;
            font-size: 15px;
            color: #333;
            border-left: 4px solid #667eea;
            display: block;
            box-shadow: 0 2px 8px rgba(102, 126, 234, 0.1);
            opacity: 0.7;
            transition: all 0.3s ease;
        }

        .team-card.active .team-result {
            opacity: 1;
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.15) 0%, rgba(118, 75, 162, 0.15) 100%);
            border-left: 4px solid #667eea;
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
            animation: slideInUp 0.3s ease-out;
        }

        .team-result .highlight {
            background: rgba(102, 126, 234, 0.2);
            padding: 3px 8px;
            border-radius: 4px;
            font-weight: 600;
            color: #667eea;
        }

        /* 结果显示区域 */
        .result-container {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 25px;
            border-radius: 15px;
            text-align: center;
            margin-top: 20px;
        }

        .result-title {
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 15px;
        }

        .result-content {
            font-size: 18px;
            line-height: 1.6;
        }

        .highlight {
            background: rgba(255, 255, 255, 0.2);
            padding: 5px 10px;
            border-radius: 6px;
            font-weight: 600;
        }

        /* 响应式设计 */
        @media (max-width: 1200px) {
            .container {
                max-width: 95%;
                margin: 10px auto;
            }
        }

        /* 响应式水印 */
        @media (max-width: 768px) {
            .tab-container::before {
                font-size: 18px;
            }
            
            .tab-content::before {
                font-size: 36px;
            }
        }

        @media (max-width: 480px) {
            .tab-container::before {
                font-size: 14px;
            }
            
            .tab-content::before {
                font-size: 28px;
            }
        }

        @media (max-width: 768px) {
            .container {
                margin: 10px;
                border-radius: 15px;
            }

            .tab-content {
                padding: 20px;
            }

            .team-management {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }

            .rounds-row {
                justify-content: center;
                gap: 8px;
            }

            .round-item {
                min-width: 60px;
            }

            .option-button {
                padding: 6px 10px;
                font-size: 12px;
                min-width: 35px;
            }

            .team-header {
                flex-direction: column;
                gap: 10px;
                align-items: flex-start;
            }

            .team-actions {
                align-self: flex-end;
            }

            .team-result {
                padding: 15px;
                font-size: 14px;
            }
        }

        @media (max-width: 480px) {
            .rounds-row {
                gap: 6px;
            }

            .round-item {
                min-width: 50px;
            }

            .option-button {
                padding: 5px 8px;
                font-size: 11px;
                min-width: 30px;
            }

            .round-label {
                font-size: 10px;
            }

            .team-card {
                padding: 20px;
            }

            .team-result {
                padding: 12px;
                font-size: 13px;
            }
        }

        /* 动画效果 */
        .round-item {
            animation: slideInUp 0.5s ease-out;
        }

        .round-item:nth-child(1) { animation-delay: 0.1s; }
        .round-item:nth-child(2) { animation-delay: 0.2s; }
        .round-item:nth-child(3) { animation-delay: 0.3s; }
        .round-item:nth-child(4) { animation-delay: 0.4s; }
        .round-item:nth-child(5) { animation-delay: 0.5s; }
        .round-item:nth-child(6) { animation-delay: 0.6s; }
        .round-item:nth-child(7) { animation-delay: 0.7s; }
        .round-item:nth-child(8) { animation-delay: 0.8s; }
        .round-item:nth-child(9) { animation-delay: 0.9s; }
        .round-item:nth-child(10) { animation-delay: 1.0s; }

        @keyframes slideInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 选项卡导航 -->
        <div class="tab-container">
            <ul class="tab-nav">
                <li>
                    <button class="tab-button active" data-tab="statistics">瓢把子统计</button>
                </li>
            </ul>
        </div>

        <!-- 选项卡内容 -->
        <div class="tab-content">
            <div id="statistics" class="tab-panel active">
                <h2 class="statistics-title">瓢把子统计</h2>
                <div class="author-info">by 内测专区-青柠秋刀鱼</div>
                
                <!-- 队伍管理区域 -->
                <div class="team-management">
                    <div class="team-controls">
                        <button class="add-team-btn" id="add-team-btn">+ 新建队伍</button>
                    </div>
                    <div class="current-team-info" id="current-team-info">
                        当前队伍：队伍1
                    </div>
                </div>

                <!-- 队伍列表容器 -->
                <div class="teams-container" id="teams-container">
                    <!-- 队伍将通过JavaScript动态生成 -->
                </div>

                <div class="result-container">
                    <div class="result-title">S镖押注建议</div>
                    <div class="result-content" id="result-content">
                        请选择各回合的瓢把子情况
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 水印防护管理器
        class WatermarkManager {
            constructor() {
                this.watermarkText = '内测专区-青柠秋刀鱼';
                this.watermarks = new Map();
                this.observer = null;
                this.checkInterval = null;
                this.isDebugging = false;
                this.init();
            }

            init() {
                this.createWatermarks();
                this.startMonitoring();
                this.startAntiDebug();
                console.log('水印防护系统已启动');
            }

            // 创建多重水印
            createWatermarks() {
                // 创建选项卡导航区域水印
                this.createWatermark('.tab-container', {
                    fontSize: '24px',
                    color: 'rgba(108, 117, 125, 0.15)',
                    zIndex: 1
                });

                // 创建选项卡内容区域水印
                this.createWatermark('.tab-content', {
                    fontSize: '48px',
                    color: 'rgba(102, 126, 234, 0.08)',
                    zIndex: 1
                });

                // 创建隐藏的备用水印
                this.createHiddenWatermarks();
            }

            // 创建单个水印
            createWatermark(selector, options = {}) {
                const element = document.querySelector(selector);
                if (!element) return;

                const watermark = document.createElement('div');
                watermark.className = 'watermark-protected';
                watermark.textContent = this.watermarkText;
                watermark.style.cssText = `
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%) rotate(-45deg);
                    font-size: ${options.fontSize || '24px'};
                    font-weight: bold;
                    color: ${options.color || 'rgba(0, 0, 0, 0.1)'};
                    z-index: ${options.zIndex || 1};
                    pointer-events: none;
                    user-select: none;
                    white-space: nowrap;
                    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
                `;

                // 添加保护属性
                watermark.setAttribute('data-watermark-id', Date.now());
                watermark.setAttribute('data-protected', 'true');

                element.appendChild(watermark);
                this.watermarks.set(selector, watermark);
            }

            // 创建隐藏的备用水印
            createHiddenWatermarks() {
                // 在body中创建隐藏的备用水印
                const hiddenWatermark = document.createElement('div');
                hiddenWatermark.className = 'watermark-backup';
                hiddenWatermark.textContent = this.watermarkText;
                hiddenWatermark.style.cssText = `
                    position: fixed;
                    top: -9999px;
                    left: -9999px;
                    opacity: 0;
                    pointer-events: none;
                    user-select: none;
                `;
                hiddenWatermark.setAttribute('data-watermark-backup', 'true');
                document.body.appendChild(hiddenWatermark);
            }

            // 开始监控
            startMonitoring() {
                // 使用MutationObserver监控DOM变化
                this.observer = new MutationObserver((mutations) => {
                    mutations.forEach((mutation) => {
                        if (mutation.type === 'childList') {
                            this.checkWatermarks();
                        }
                    });
                });

                this.observer.observe(document.body, {
                    childList: true,
                    subtree: true,
                    attributes: true,
                    attributeFilter: ['style', 'class']
                });

                // 定期检查水印
                this.checkInterval = setInterval(() => {
                    this.checkWatermarks();
                }, 1000);
            }

            // 检查水印状态
            checkWatermarks() {
                this.watermarks.forEach((watermark, selector) => {
                    if (!watermark.isConnected || !watermark.textContent.includes(this.watermarkText)) {
                        console.warn(`检测到水印被修改: ${selector}`);
                        this.restoreWatermark(selector);
                    }
                });
            }

            // 恢复水印
            restoreWatermark(selector) {
                const element = document.querySelector(selector);
                if (!element) return;

                // 移除旧的水印
                const oldWatermark = element.querySelector('.watermark-protected');
                if (oldWatermark) {
                    oldWatermark.remove();
                }

                // 重新创建水印
                const options = this.getWatermarkOptions(selector);
                this.createWatermark(selector, options);
                
                console.log(`水印已恢复: ${selector}`);
            }

            // 获取水印选项
            getWatermarkOptions(selector) {
                const optionsMap = {
                    '.tab-container': {
                        fontSize: '24px',
                        color: 'rgba(108, 117, 125, 0.15)',
                        zIndex: 1
                    },
                    '.tab-content': {
                        fontSize: '48px',
                        color: 'rgba(102, 126, 234, 0.08)',
                        zIndex: 1
                    }
                };
                return optionsMap[selector] || {};
            }

            // 反调试检测
            startAntiDebug() {
                // 检测开发者工具
                let devtools = false;
                const threshold = 160;

                setInterval(() => {
                    if (window.outerHeight - window.innerHeight > threshold || 
                        window.outerWidth - window.innerWidth > threshold) {
                        if (!devtools) {
                            devtools = true;
                            console.warn('检测到开发者工具打开');
                            this.onDebugDetected();
                        }
                    } else {
                        devtools = false;
                    }
                }, 500);

                // 检测console.clear
                const originalClear = console.clear;
                console.clear = () => {
                    console.warn('检测到console.clear调用');
                    originalClear.apply(console);
                };

                // 检测debugger语句
                setInterval(() => {
                    const start = performance.now();
                    debugger;
                    const end = performance.now();
                    if (end - start > 100) {
                        console.warn('检测到调试器');
                        this.onDebugDetected();
                    }
                }, 1000);
            }

            // 调试检测回调
            onDebugDetected() {
                // 重新创建所有水印
                this.watermarks.clear();
                this.createWatermarks();
                console.log('检测到调试，水印已重新创建');
            }

            // 销毁管理器
            destroy() {
                if (this.observer) {
                    this.observer.disconnect();
                }
                if (this.checkInterval) {
                    clearInterval(this.checkInterval);
                }
                this.watermarks.clear();
            }
        }

        // 多队伍游戏状态管理
        class MultiTeamGameStatistics {
            constructor() {
                this.teams = []; // 队伍数组
                this.currentTeamId = null; // 当前选中的队伍ID
                this.teamCounter = 1; // 队伍计数器
                this.init();
            }

            init() {
                if (this.teams.length === 0) {
                    this.addTeam(); // 默认创建一个队伍
                }
                this.createTeamElements();
                this.bindEvents();
                this.updateCurrentTeamInfo();
            }

            // 创建队伍元素
            createTeamElements() {
                const container = document.getElementById('teams-container');
                container.innerHTML = '';

                console.log(`重新创建队伍元素，队伍数量: ${this.teams.length}`);
                
                this.teams.forEach(team => {
                    const teamElement = this.createTeamCard(team);
                    container.appendChild(teamElement);
                    console.log(`创建队伍卡片: ${team.name}, 数据状态:`, team.rounds);
                });

                // 恢复所有队伍的按钮状态
                this.teams.forEach(team => {
                    this.updateTeamRoundDisplay(team);
                });
            }

            // 创建单个队伍卡片
            createTeamCard(team) {
                const teamCard = document.createElement('div');
                teamCard.className = `team-card ${team.id === this.currentTeamId ? 'active' : ''}`;
                teamCard.dataset.teamId = team.id;

                // 创建回合行
                const roundsRow = document.createElement('div');
                roundsRow.className = 'rounds-row';

                for (let i = 1; i <= 10; i++) {
                    const roundItem = document.createElement('div');
                    roundItem.className = 'round-item';
                    roundItem.innerHTML = `
                        <div class="round-label">第${i}回合</div>
                        <div class="round-options">
                            <button class="option-button" data-team-id="${team.id}" data-round="${i}" data-value="come">√</button>
                            <button class="option-button" data-team-id="${team.id}" data-round="${i}" data-value="not-come">×</button>
                        </div>
                    `;
                    roundsRow.appendChild(roundItem);
                }

                // 队伍结果显示
                const teamResult = document.createElement('div');
                teamResult.className = 'team-result';
                teamResult.innerHTML = this.calculateTeamSuggestion(team);

                teamCard.innerHTML = `
                    <div class="team-header">
                        <div class="team-name" data-team-id="${team.id}">${team.name}</div>
                        <div class="team-actions">
                            <button class="team-action-btn reset-btn" data-team-id="${team.id}">重置</button>
                            <button class="team-action-btn rename-btn" data-team-id="${team.id}">重命名</button>
                            <button class="team-action-btn delete-btn" data-team-id="${team.id}">删除</button>
                        </div>
                    </div>
                `;

                teamCard.appendChild(roundsRow);
                teamCard.appendChild(teamResult);

                // 更新回合显示
                this.updateTeamRoundDisplay(team);

                return teamCard;
            }

            // 绑定事件
            bindEvents() {
                document.addEventListener('click', (e) => {
                    // 新建队伍按钮
                    if (e.target.id === 'add-team-btn') {
                        this.addTeam();
                    }
                    // 回合选择按钮
                    else if (e.target.classList.contains('option-button')) {
                        const teamId = e.target.dataset.teamId;
                        const round = parseInt(e.target.dataset.round);
                        const value = e.target.dataset.value;
                        this.selectRound(teamId, round, value);
                    }
                    // 重置按钮
                    else if (e.target.classList.contains('reset-btn')) {
                        const teamId = e.target.dataset.teamId;
                        this.resetTeam(teamId);
                    }
                    // 重命名按钮
                    else if (e.target.classList.contains('rename-btn')) {
                        const teamId = e.target.dataset.teamId;
                        this.startRenameTeam(teamId);
                    }
                    // 删除按钮
                    else if (e.target.classList.contains('delete-btn')) {
                        const teamId = e.target.dataset.teamId;
                        this.deleteTeam(teamId);
                    }
                    // 队伍卡片点击（放在最后，避免拦截按钮事件）
                    else if (e.target.closest('.team-card')) {
                        const teamCard = e.target.closest('.team-card');
                        const teamId = teamCard.dataset.teamId;
                        this.selectTeam(teamId);
                    }
                });

                // 队伍名称编辑事件
                document.addEventListener('blur', (e) => {
                    if (e.target.classList.contains('team-name') && e.target.classList.contains('editing')) {
                        this.finishRenameTeam(e.target);
                    }
                });

                document.addEventListener('keypress', (e) => {
                    if (e.target.classList.contains('team-name') && e.target.classList.contains('editing')) {
                        if (e.key === 'Enter') {
                            this.finishRenameTeam(e.target);
                        }
                    }
                });
            }

            // 添加队伍
            addTeam() {
                const newTeam = {
                    id: `team_${this.teamCounter++}`,
                    name: `队伍${this.teams.length + 1}`,
                    rounds: Array(10).fill(null)
                };
                this.teams.push(newTeam);
                this.currentTeamId = newTeam.id;
                
                // 只重新创建DOM，因为添加了新队伍
                this.createTeamElements();
                this.updateCurrentTeamInfo();
                
                console.log(`添加新队伍: ${newTeam.name}, 当前队伍总数: ${this.teams.length}`);
            }

            // 选择队伍
            selectTeam(teamId) {
                // 如果已经是当前队伍，不需要切换
                if (this.currentTeamId === teamId) {
                    return;
                }
                
                this.currentTeamId = teamId;
                
                // 只更新激活状态，不重新创建DOM
                this.updateTeamActiveState();
                this.updateCurrentTeamInfo();
                this.updateMainResult();
                
                // 确保当前队伍的回合状态正确显示
                const currentTeam = this.teams.find(t => t.id === this.currentTeamId);
                if (currentTeam) {
                    this.updateTeamRoundDisplay(currentTeam);
                }
            }
            
            // 更新队伍激活状态
            updateTeamActiveState() {
                // 移除所有队伍的active类
                document.querySelectorAll('.team-card').forEach(card => {
                    card.classList.remove('active');
                });
                
                // 为当前队伍添加active类
                const currentTeamCard = document.querySelector(`[data-team-id="${this.currentTeamId}"]`);
                if (currentTeamCard) {
                    currentTeamCard.classList.add('active');
                }
            }

            // 获取当前已选择的回合数
            getCurrentRound(team) {
                // 计算已选择的回合数（非null的回合数）
                let selectedCount = 0;
                for (let i = 0; i < 10; i++) {
                    if (team.rounds[i] !== null) {
                        selectedCount++;
                    } else {
                        break; // 遇到第一个null就停止计数
                    }
                }
                return selectedCount;
            }

            // 获取已选择的瓢把子次数
            getComeCount(team) {
                return team.rounds.filter(round => round === 'come').length;
            }

            // 获取瓢把子出现的回合
            getComeRounds(team) {
                const comeRounds = [];
                for (let i = 0; i < 10; i++) {
                    if (team.rounds[i] === 'come') {
                        comeRounds.push(i + 1); // 回合数从1开始
                    }
                }
                return comeRounds;
            }

            // 选择回合状态
            selectRound(teamId, round, value) {
                const team = this.teams.find(t => t.id === teamId);
                if (team) {
                    // 检查是否在修改已选择的回合
                    const isModifyingExisting = team.rounds[round - 1] !== null;
                    
                    if (!isModifyingExisting) {
                        // 新选择回合，需要按顺序验证
                        const currentRound = this.getCurrentRound(team);
                        const nextRound = currentRound + 1; // 下一个应该选择的回合数
                        if (round !== nextRound) {
                            this.showSuccessMessage(`请按顺序选择！当前应该选择第${nextRound}回合`);
                            return;
                        }
                    }
                    // 如果是修改已选择的回合，直接允许
                    
                    team.rounds[round - 1] = value;
                    this.updateTeamRoundDisplay(team);
                    this.updateTeamResult(team);
                    this.updateMainResult();
                }
            }

            // 更新队伍回合显示
            updateTeamRoundDisplay(team) {
                const teamCard = document.querySelector(`[data-team-id="${team.id}"]`);
                if (!teamCard) {
                    console.log('未找到队伍卡片:', team.id);
                    return;
                }

                // 清空所有回合按钮的状态
                const allButtons = teamCard.querySelectorAll('.option-button');
                console.log(`队伍 ${team.name} 清空按钮状态，找到按钮数量:`, allButtons.length);
                allButtons.forEach(button => {
                    button.classList.remove('selected', 'come', 'not-come');
                });

                // 根据数据状态设置按钮状态
                for (let i = 1; i <= 10; i++) {
                    const selectedValue = team.rounds[i - 1];
                    if (selectedValue) {
                        const selectedButton = teamCard.querySelector(`[data-round="${i}"][data-value="${selectedValue}"]`);
                        if (selectedButton) {
                            selectedButton.classList.add('selected', selectedValue);
                            console.log(`队伍 ${team.name} 第${i}回合设置为:`, selectedValue);
                        } else {
                            console.warn(`队伍 ${team.name} 第${i}回合按钮未找到:`, selectedValue);
                        }
                    }
                }
                
                console.log(`队伍 ${team.name} 回合显示更新完成，数据状态:`, team.rounds);
            }

            // 更新队伍结果显示
            updateTeamResult(team) {
                const teamCard = document.querySelector(`[data-team-id="${team.id}"]`);
                if (!teamCard) return;

                const teamResult = teamCard.querySelector('.team-result');
                if (teamResult) {
                    teamResult.innerHTML = this.calculateTeamSuggestion(team);
                }
            }

            // 计算队伍S镖押注建议
            calculateTeamSuggestion(team) {
                const currentRound = this.getCurrentRound(team);
                const comeCount = this.getComeCount(team);
                const comeRounds = this.getComeRounds(team);
                const teamName = team.name;

                // 无选择时
                if (currentRound === 0) {
                    return `[${teamName}] 请从第一回合开始选择`;
                }

                // 如果达到5次瓢把子
                if (comeCount >= 5) {
                    const lastComeRound = comeRounds[4]; // 第5次瓢把子的回合
                    const remainingRounds = 10 - lastComeRound;
                    const sBiaoCount = Math.max(0, remainingRounds);
                    
                    if (sBiaoCount > 0) {
                        return `[${teamName}] 第${lastComeRound}回合达到5次瓢把子，现在可以押<span class="highlight">${sBiaoCount}次S镖</span>`;
                    } else {
                        return `[${teamName}] 已满足5次瓢把子，但剩余回合不足，请谨慎选择押镖等级`;
                    }
                }

                // 如果所有回合都已选择但未达到5次瓢把子
                if (currentRound >= 10) {
                    return `[${teamName}] 第10回合结束，瓢把子不足5次，<span class="highlight">S镖中可能会遇到瓢把子</span>，建议谨慎押注`;
                }

                // 使用新的提示格式
                const neededCount = Math.max(0, 5 - comeCount);
                return `[${teamName}] 当前为第<span class="highlight">${currentRound}</span>回合，已遇到瓢把子<span class="highlight">${comeCount}</span>次，还需要遇到<span class="highlight">${neededCount}</span>次瓢把子`;
            }

            // 更新主结果显示
            updateMainResult() {
                const currentTeam = this.teams.find(t => t.id === this.currentTeamId);
                if (!currentTeam) return;

                const resultElement = document.getElementById('result-content');
                const suggestion = this.calculateTeamSuggestion(currentTeam);
                
                resultElement.innerHTML = suggestion;
            }

            // 更新当前队伍信息
            updateCurrentTeamInfo() {
                const currentTeam = this.teams.find(t => t.id === this.currentTeamId);
                const infoElement = document.getElementById('current-team-info');
                if (currentTeam && infoElement) {
                    infoElement.textContent = `当前队伍：${currentTeam.name}`;
                }
            }

            // 开始重命名队伍
            startRenameTeam(teamId) {
                const teamNameElement = document.querySelector(`[data-team-id="${teamId}"].team-name`);
                if (teamNameElement) {
                    teamNameElement.contentEditable = true;
                    teamNameElement.classList.add('editing');
                    teamNameElement.focus();
                    // 选中文本
                    const range = document.createRange();
                    range.selectNodeContents(teamNameElement);
                    const selection = window.getSelection();
                    selection.removeAllRanges();
                    selection.addRange(range);
                }
            }

            // 完成重命名队伍
            finishRenameTeam(teamNameElement) {
                const teamId = teamNameElement.dataset.teamId;
                const newName = teamNameElement.textContent.trim();
                
                if (newName) {
                    const team = this.teams.find(t => t.id === teamId);
                    if (team) {
                        const oldName = team.name;
                        team.name = newName;
                        this.updateCurrentTeamInfo();
                        
                        // 更新该队伍的结果显示
                        this.updateTeamResult(team);
                        
                        // 如果重命名的是当前激活队伍，更新主结果显示
                        if (this.currentTeamId === teamId) {
                            this.updateMainResult();
                        }
                        
                        // 显示成功提示
                        this.showSuccessMessage('队伍已重命名为"' + newName + '"');
                    }
                }

                teamNameElement.contentEditable = false;
                teamNameElement.classList.remove('editing');
            }

            // 重置队伍
            resetTeam(teamId) {
                const team = this.teams.find(t => t.id === teamId);
                if (!team) return;

                if (confirm('确定要重置队伍"' + team.name + '"的所有选择吗？\n\n这将清空所有回合的瓢把子选择。')) {
                    // 重置队伍的所有回合选择
                    team.rounds = Array(10).fill(null);
                    
                    // 重新创建队伍元素，确保DOM状态与数据状态同步
                    this.createTeamElements();
                    
                    // 强制更新队伍结果显示
                    this.updateTeamResult(team);
                    
                    // 更新当前队伍信息
                    this.updateCurrentTeamInfo();
                    
                    // 更新主结果显示
                    this.updateMainResult();
                    
                    // 显示成功提示
                    this.showSuccessMessage('队伍"' + team.name + '"已重置');
                    
                    console.log('队伍重置完成，所有选择已清空');
                }
            }

            // 删除队伍
            deleteTeam(teamId) {
                if (this.teams.length <= 1) {
                    alert('至少需要保留一个队伍！');
                    return;
                }

                const team = this.teams.find(t => t.id === teamId);
                if (!team) return;

                if (confirm('确定要删除队伍"' + team.name + '"吗？\n\n删除后无法恢复，请谨慎操作。')) {
                    this.teams = this.teams.filter(t => t.id !== teamId);
                    
                    // 如果删除的是当前队伍，选择第一个队伍
                    if (this.currentTeamId === teamId) {
                        this.currentTeamId = this.teams[0].id;
                    }
                    
                    this.createTeamElements();
                    this.updateCurrentTeamInfo();
                    this.updateMainResult();
                    
                    // 显示成功提示
                    this.showSuccessMessage('队伍"' + team.name + '"已删除');
                }
            }

            // 显示成功提示消息
            showSuccessMessage(message) {
                // 创建提示元素
                const toast = document.createElement('div');
                toast.style.cssText = `
                    position: fixed;
                    top: 20px;
                    right: 20px;
                    background: #28a745;
                    color: white;
                    padding: 12px 20px;
                    border-radius: 8px;
                    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
                    z-index: 1000;
                    font-size: 14px;
                    font-weight: 500;
                    opacity: 0;
                    transform: translateX(100%);
                    transition: all 0.3s ease;
                `;
                toast.textContent = message;
                
                document.body.appendChild(toast);
                
                // 显示动画
                setTimeout(() => {
                    toast.style.opacity = '1';
                    toast.style.transform = 'translateX(0)';
                }, 100);
                
                // 自动隐藏
                setTimeout(() => {
                    toast.style.opacity = '0';
                    toast.style.transform = 'translateX(100%)';
                    setTimeout(() => {
                        if (toast.parentNode) {
                            toast.parentNode.removeChild(toast);
                        }
                    }, 300);
                }, 3000);
            }

        }

        // 初始化多队伍游戏统计和水印防护
        document.addEventListener('DOMContentLoaded', () => {
            // 初始化水印防护系统
            window.watermarkManager = new WatermarkManager();
            
            // 初始化游戏统计系统
            new MultiTeamGameStatistics();
        });
    </script>
</body>
</html>
